package cn.wolfcode.wolf2w.util;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

@ControllerAdvice//控制器增强
public class CommonExceptionHandler {

    @ExceptionHandler(LogicException.class)//出现异常类型
    @ResponseBody
    public Object logicException(Exception e, HttpServletResponse resp) {
        e.printStackTrace();// 一定要打印捕获到的异常信息
        resp.setContentType("text/json;charset=utf-8");
        return new JsonResult(JsonResult.CODE_ERROR,e.getMessage(),null);
    }

    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public Object runtimeException(Exception e, HttpServletResponse resp) {
        e.printStackTrace();
        resp.setContentType("text/json;charset=utf-8");
        return JsonResult.defaultError();
    }
}
